package com.xiaoyao.novel.action.page;

import java.util.List;

import org.apache.log4j.Logger;

import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Category;

public class CategoryAction extends NovelAction {
	private static Logger logger = Logger.getLogger(CategoryAction.class);
	private static final long serialVersionUID = 1L;
	Category category;

	public String list() throws Exception {
		List<Book> bookList = categoryService.getCategoryBooks(category.getCateId());
		category = categoryService.getById(category.cateId);
		request.setAttribute("bookList", bookList);
		request.setAttribute("category", category);
		if (bookList.size()>0) {
			Book book =bookList.get(0);
			request.setAttribute("book", book);
		}
		getConfig();
		listCategory();
		getFooter();
		return SUCCESS;
	}

	@Override
	public String execute() throws Exception {
		List<Category> list = categoryService.getAll();
		request.setAttribute("list", list);
		return SUCCESS;
	}

	public String toUpdate() throws Exception {
		category = categoryService.getById(category.cateId);
		request.setAttribute("category", category);
		return "toUpdate";
	}

	public String update() throws Exception {
		Category oldCategory= categoryService.getById(category.getCateId());
		category.setCateNameOld(oldCategory.getCateName());
 		categoryService.updateBookCategory(category );//修改书籍的分类
 		categoryService.update(category);//修改分类
		return execute();
	}

	public String addCategory() throws Exception {
		categoryService.insert(category);
		return execute();
	}

	public String delCategory() throws Exception {
		categoryService.delete(category.cateId);
		return execute();
	}

	// //////////////////////////
	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		CategoryAction.logger = logger;
	}
  
	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
